package com.itheima.controller;

import com.itheima.Service.OrderService;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderQueryParam;
import com.itheima.pojo.StatusUpdateParam;
import com.itheima.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/orders")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 分页查询订单列表
     */
    @GetMapping
    public Result page(OrderQueryParam orderQueryParam) {
        log.info("分页查询订单：{}", orderQueryParam);
        return Result.success(orderService.page(orderQueryParam));
    }

    /**
     * 获取订单详情
     */
    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id) {
        log.info("查询订单详情，ID：{}", id);
        return Result.success(orderService.get(id));
    }

    /**
     * 添加订单
     */
    @PostMapping
    public Result add(@RequestBody Order order) {
        log.info("添加订单：{}", order);
        orderService.add(order);
        return Result.success();
    }

    /**
     * 更新订单
     */
    @PutMapping()
    public Result update(@RequestBody Order order) {
        log.info("更新订单信息：{}", order);
        orderService.update(order);
        return Result.success();
    }

    /**
     * 更新订单状态
     */
    @PutMapping("/{id}/status")
    public Result updateStatus(@PathVariable Integer id, @RequestBody StatusUpdateParam statusUpdateParam) {
        log.info("更新订单状态，ID：{}，状态：{}", id, statusUpdateParam.getStatus());
        orderService.updateStatus(id, statusUpdateParam.getStatus());
        return Result.success();
    }

    /**
     * 删除订单
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除订单，ID：{}", id);
        orderService.delete(id);
        return Result.success();
    }
}